import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../theme/xlcolors.dart';
import '../theme/xlsize.dart';

class BaseDialog extends StatefulWidget {
  const BaseDialog({
    Key? key,
    this.titleText,
    this.title,
    this.titleStyle,
    this.content,
    this.contentText,
    this.leftText,
    this.rightText,
    this.leftStyle,
    this.rightStyle,
    this.outCancel = true,
    this.circularSize = 12,
    this.onClickLeft,
    this.onClickRight,
  }) : super(key: key);
  final String? titleText;
  final Widget? title;
  final TextStyle? titleStyle;
  final Widget? content;
  final String? contentText;
  final String? leftText;
  final String? rightText;
  final TextStyle? leftStyle;
  final TextStyle? rightStyle;
  final bool outCancel;
  final double circularSize;
  final Function()? onClickLeft;
  final Function()? onClickRight;

  @override
  _BaseDialogState createState() => _BaseDialogState();
}

class _BaseDialogState extends State<BaseDialog> {
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Container(
        color: Colors.transparent,
        alignment: Alignment.center,
        child: GestureDetector(
          onTap: () {},
          child: Container(
            width: double.infinity,
            margin: const EdgeInsets.only(left: 20, right: 20),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.all(
                Radius.circular(widget.circularSize),
              ),
            ),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                SizedBox(
                  height: 28,
                ),
                Visibility(
                  visible: widget.titleText != null,
                  child: Container(
                    child: Text(
                      widget.titleText ?? "",
                      style: widget.titleStyle ??
                          TextStyle(fontSize: 18, fontWeight: FontWeight.w500),
                    ),
                  ),
                ),
                Visibility(
                  visible: widget.title != null,
                  child: widget.title ?? Text(""),
                ),
                Container(
                  padding: EdgeInsets.only(left: 18, right: 18, top: 16),
                  child: widget.content == null
                      ? Text(
                          widget.contentText ?? "",
                          style: TextStyle(
                              fontSize: XlSize.textLage,
                              fontWeight: FontWeight.w400,
                              color: XlColors.colorA6),
                        )
                      : widget.content,
                ),
                SizedBox(
                  height: 24,
                ),
                Divider(
                  color: XlColors.dividerColor,
                  height: 0.5,
                ),
                Container(
                  height: 54,
                  child: Row(
                    children: [
                      Expanded(
                          flex: widget.leftText != null ? 1 : 0,
                          child: Visibility(
                            visible: widget.leftText != null,
                            child: TextButton(
                              onPressed: () {
                                Get.back();
                                if (widget.onClickLeft != null) {
                                  widget.onClickLeft!();
                                }
                              },
                              child: Text(
                                widget.leftText ?? "取消",
                                style: widget.leftStyle ??
                                    TextStyle(
                                      color: XlColors.colorA6,
                                      fontSize: 18,
                                    ),
                              ),
                            ),
                          )),
                      Container(
                        height: double.infinity,
                        color: XlColors.dividerColor,
                        width: (widget.rightText == null ||
                                widget.leftText == null)
                            ? 0
                            : 0.5,
                      ),
                      Expanded(
                        flex: widget.rightText != null ? 1 : 0,
                        child: Visibility(
                          visible: widget.rightText != null,
                          child: TextButton(
                            onPressed: () {
                              Get.back();
                              if (widget.onClickRight != null) {
                                widget.onClickRight!();
                              }
                            },
                            child: Text(
                              widget.rightText ?? "确定",
                              style: widget.rightStyle ??
                                  TextStyle(
                                      color: XlColors.color1890, fontSize: 18),
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                )
              ],
            ),
          ),
        ),
      ),
      onTap: () {
        if (widget.outCancel) {
          Get.back();
        }
      },
    );
  }
}
